<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>todolist效果</title>
    <link rel="stylesheet" href="./reset.css">
    <style>
        .bigbox {
            width: 360px;
            height: 600px;
            margin: 50px auto;
            border: 1px solid gray;
            border-radius: 10px;
        }

        .top input {
            width: 300px;
            height: 40px;
            background-color: antiquewhite;
            margin-top: 10px;
            margin-left: 10px;
            border-radius: 10px;
            padding-left: 10px;
        }

        .top span {
            color: rgb(37, 218, 188);
        }

        .form {
            display: flex;
            justify-content: space-around;
            margin: 10px;
        }

        .form .btn {
            background-color: rgb(37, 218, 188);
            height: 30px;
            border-radius: 5px;
            line-height: 30px;
        }

        .form .btn:nth-child(3) {
            background-color: red;
        }

        .liebiao {
            margin-left: 10px;
            font-weight: 700;
        }

        .list .task {
            display: flex;
            width: 340px;
            height: 40px;
            background-color: red;
            color: white;
            margin: 10px;
            align-items: center;
            justify-content: space-around;
        }

        .list .task.finished {
            background-color: lightseagreen;
        }

        .empty {
            text-align: center;
            margin: 50px 0;
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="bigbox">
            <div class="top">
                <input type="text" placeholder="输入任务名称" v-model="inputText">
                <span @click="add">新增</span>
            </div>
            <div class="form">
                <div class="btn">
                    <span>全部任务({{tasklist.length}})</span>
                </div>
                <div class="btn">
                    <span>已完成({{ finishcount}})</span>
                </div>
                <div class="btn">
                    <span>未完成({{ unfinishcount}})</span>
                </div>

            </div>
            <div class="liebiao">任务列表</div>
            <div class="list">
                <div v-bind:class="{ task: true, finished: item.status }" v-for="(item,index) in tasklist" :key="index">
                    <input type="checkbox" @click="changeStatus(item)">
                    <div class="name">
                        <span>{{item.order}}</span>
                        <span>{{item.name}}</span>
                    </div>
                    <span class="btn" @click="deleteTask(item)">删除</span>
                </div>
                <div class="empty" v-show="tasklist.length == 0">任务列表为空，请添加一个任务！</div>
            </div>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            inputText: '',
            tasklist: []
        },
        methods: {
            add() {
                if (this.inputText) {
                    // 构建一个对象
                    var obj = { id: this.tasklist.length + 1, order: this.tasklist.length + 1, name: this.inputText, status: false }
                    this.tasklist.push(obj)
                    this.inputText = ''
                }
            },
            changeStatus(item) {
                item.status = !item.status;
            },
            deleteTask(item){
                var i = this.tasklist.findIndex((it)=>{
                    return it.id = item.id
                })
                if(i != -1){
                    this.tasklist.splice(i,1)
                }
            } 
        },
        computed:{
            finishcount(){
                return this.tasklist.filter((item)=>{ return item.status }).length;
            },
            unfinishcount(){
                return this.tasklist.length - this.finishcount
            }
        }
    })
</script>